package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.model.entity.Anonname;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.AnonnameService;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/anonNameControl")
@Slf4j
public class AnonNameController {

    @Resource
    private AnonnameService anonnameService;

    @GetMapping("/addanonName")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public void addAnonName(@RequestParam String anonname) {
        String [] list = anonname.split(" ");
        for (String anonname1 : list) {
            Anonname anonname2 = new Anonname();
            anonname2.setAnonname(anonname1);
            anonnameService.save(anonname2);
        }

    }

    @Resource
    UserService userService;
    @GetMapping("/getmyanonname")
    public BaseResponse<String> getMyAnonName(HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(loginUser.getAnon_name());
    }

    @GetMapping("/resetanonname")
    public BaseResponse<String> resetAnonName(HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        String newAnonName = anonnameService.getAnonname();
        loginUser.setAnon_name(newAnonName);
        userService.updateById(loginUser);

        return ResultUtils.success(newAnonName);

    }
}
